﻿// <copyright file="TranslateTransform.cs" company="Axelerate Solutions S.A.">
// Copyright (c) Axelerate Solutions S.A.  All rights reserved.
// </copyright>

namespace Axelerate.Silverlight3D.Media.Media3D
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    
    /// <summary>
    /// File name: TranslateTransform3D.cs
    /// Company: Axelerate Solutions S.A.
    /// Created By: Daniel Rojas
    /// Created Date: 10/16/2008 12:31:00 h.
    /// Description: Translates an object in the three-dimensional x-y-z plane.
    ///   <Modifications>
    ///   </Modifications>
    /// </summary>

    public sealed class TranslateTransform3D : AffineTransform3D
    {
        #region Fields
        
        /// <summary>
        /// Identifies the OffsetX dependency property.
        /// </summary>
        public static readonly DependencyProperty OffsetXProperty =
                    DependencyProperty.Register(
                          "OffsetX",
                          typeof(double),
                          typeof(TranslateTransform3D),
                          new PropertyMetadata(OnOffsetXPropertyChanged));

        /// <summary>
        /// Identifies the OffsetY dependency property.
        /// </summary>
        public static readonly DependencyProperty OffsetYProperty =
                    DependencyProperty.Register(
                          "OffsetY",
                          typeof(double),
                          typeof(TranslateTransform3D),
                          new PropertyMetadata(OnOffsetYPropertyChanged));

        /// <summary>
        /// Identifies the OffsetZ dependency property.
        /// </summary>
        public static readonly DependencyProperty OffsetZProperty =
                    DependencyProperty.Register(
                          "OffsetZ",
                          typeof(double),
                          typeof(TranslateTransform3D),
                          new PropertyMetadata(OnOffsetZPropertyChanged));
        #endregion

        #region Properties
        
        /// <summary>
        /// Gets a Matrix3D that represents the value of the translation.
        /// </summary>
        public override Matrix3D Value
        {
            get
            {
                //TODO: Cache this - implement Freezable mechanism?
                return new Matrix3D(1, 0, 0, 0,
                                    0, 1, 0, 0,
                                    0, 0, 1, 0,
                                    OffsetX, OffsetY, OffsetZ, 1);
            }
        }

        /// <summary>
        /// Gets or sets the X-axis value of the translation's offset. This is a dependency property.
        /// </summary>
        public double OffsetX
        {
            get
            {

                return (double)this.GetValue(OffsetXProperty);
            }
            set
            {
                this.SetValue(OffsetXProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the Y-axis value of the translation's offset. This is a dependency property.
        /// </summary>
        public double OffsetY
        {
            get
            {

                return (double)this.GetValue(OffsetYProperty);
            }
            set
            {
                this.SetValue(OffsetYProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the Z-axis value of the translation's offset. This is a dependency property.
        /// </summary>
        public double OffsetZ
        {
            get
            {

                return (double)this.GetValue(OffsetZProperty);
            }
            set
            {
                this.SetValue(OffsetZProperty, value);
            }
        }
        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the TranslateTransform3D class.
        /// </summary>
        public TranslateTransform3D()
        {
            this.OffsetX = 0;
            this.OffsetY = 0;
            this.OffsetZ = 0;
        }

        /// <summary>
        /// Initializes a new instance of the TranslateTransform3D class, using the specified offset Vector3D.
        /// </summary>
        /// <param name="offset">Vector3D by which to offset the model.</param>
        public TranslateTransform3D(Vector3D offset)
        {
            this.OffsetX = offset.X;
            this.OffsetY = offset.Y;
            this.OffsetZ = offset.Z;
        }

        /// <summary>
        /// Initializes a new instance of the TranslateTransform3D class using the specified offset.
        /// </summary>
        /// <param name="offsetX">Double that specifies the X value of the Vector3D that specifies the translation offset.</param>
        /// <param name="offsetY">Double that specifies the Y value of the Vector3D that specifies the translation offset.</param>
        /// <param name="offsetZ">Double that specifies the Z value of the Vector3D that specifies the translation offset.</param>
        public TranslateTransform3D(double offsetX, double offsetY, double offsetZ)
        {
            this.OffsetX = offsetX;
            this.OffsetY = offsetY;
            this.OffsetZ = offsetZ;
        }
        #endregion

        #region Methods
        
        /// <summary>
        /// Raise the OffsetX changed event
        /// </summary>
        /// <param name="d">DependencyObject owner of the property</param>
        /// <param name="e">DependencyPropertyChangedEventArgs with the necesary information</param>
        private static void OnOffsetXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TranslateTransform3D)d).NotifyPropertyChanged("OffsetX");
        }

        /// <summary>
        /// Raise the OffsetX changed event
        /// </summary>
        /// <param name="d">DependencyObject owner of the property</param>
        /// <param name="e">DependencyPropertyChangedEventArgs with the necesary information</param>
        private static void OnOffsetYPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TranslateTransform3D)d).NotifyPropertyChanged("OffsetY");
        }

        /// <summary>
        /// Raise the OffsetX changed event
        /// </summary>
        /// <param name="d">DependencyObject owner of the property</param>
        /// <param name="e">DependencyPropertyChangedEventArgs with the necesary information</param>
        private static void OnOffsetZPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TranslateTransform3D)d).NotifyPropertyChanged("OffsetZ");
        }
        #endregion
    }
}
